// pages/news/index.js
var utils = require('../../utils/util.js');

var requestPath = utils.requestPath;
var currentTime = utils.currentTime;
var startParams = utils.startParams;
var lengthParams = utils.lengthParams;
var topFlag = true;

Page({
  data:{
    newsObjs: [], 
    tops: []
  },
  //事件处理函数
  detail:function(event){
    //带id跳转到指定的页面，这里的event.currentTarget.id是获取wxml页面上的id参数，详见事件说明
    wx.navigateTo({
        //url:"../detail/detail?id="+event.currentTarget.id
      url: "../detailWebView/detailWebView?id=" + event.currentTarget.id + "&carouselImage=" + event.currentTarget.dataset.carouselimage + "&title=" + event.currentTarget.dataset.title
    })
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this; 
    GetdownList(that);
  },
  onReachBottom: function() {
    // Do something when page reach bottom.
     GetupList(this);
  },
  onPullDownRefresh: function(){
    //下拉刷新
    wx.showNavigationBarLoading()
    startParams = 5;
    this.setData({
      newsObjs: [],
      tops: []
    });
    GetdownList(this)
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  onShareAppMessage: function () {
    return {
      title: '我们临空港',
      desc: '我们临空港!',
      path: '/pages/news/index'
    }
  }
})

// 上啦
var GetupList = function (that) {
  wx.request({
    url: requestPath + 'api/oneUpList',
    data: {
      releaseAt: currentTime,
      start: startParams,
      length: lengthParams
    },
    success: function (res) {
      var list = that.data.newsObjs;
      for (var i = 0; i < res.data.list.length; i++) {
        list.push(res.data.list[i]);
      }
      
      that.setData({
        newsObjs: list
      });
      startParams += lengthParams;
    }
  });
}

// 下拉刷新
var GetdownList = function (that) {
  wx.request({
    url: requestPath + 'api/oneDownList',
    data: {
      releaseAt: "2001-1-1 00:00:00"
    },
    success: function (res) {
      if (res.data.tops == null || res.data.tops.length==0){
        that.setData({
          newsObjs: res.data.list,
          topFlag : false
        });
      }else{
        that.setData({
          newsObjs: res.data.list,
          tops: res.data.tops,
          topFlag : true
        });
      }
    },
    complete: function () {
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }
  });
}